<?php
/**
 * Created by PhpStorm.
 * User: insgeek
 * Date: 2017/2/10
 * Time: 10:45
 * 抽象工厂模式
 */
namespace DesignPattern\AbstractFactoryPattern;

interface Shape
{
    public  function draw();
}

class Rectangle implements Shape
{
    public  function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Rectangle::draw() method.");
    }
}

class Square implements Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Square::draw() method.");
    }
}

class Circle implements Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Circle::draw() method.");
    }
}

interface Color
{
    public function fill();
}

class Red implements Color
{
    public function fill()
    {
        // TODO: Implement fill() method.
        print_r("Inside Red::fill() method.");
    }
}

class Green implements Color
{
    public function fill()
    {
        // TODO: Implement fill() method.
        print_r("Inside Green::fill() method.");
    }
}

class Blue implements Color
{
    public function fill()
    {
        // TODO: Implement fill() method.
        print_r("Inside Blue::fill() method.");
    }
}

abstract class AbstractFactory
{
    abstract public function getColor(string $color);

    abstract public function getShape(string $shape);
}

class ShapeFactory extends AbstractFactory
{
    public function getColor(string $color)
    {
        // TODO: Implement getColor() method.
        return null;
    }

    public function getShape(string $shape)
    {
        // TODO: Implement getShape() method.
        if ($shape == null) {
            return null;
        }
        $type = strtoupper($shape);
        if ($type == "CIRCLE") {
            return new Circle();
        } else if ($type == "RECTANGLE") {
            return new Rectangle();
        } else if ($type == "SQUARE") {
            return new Square();
        }
        return null;
    }
}

class ColorFactory extends AbstractFactory
{
    public function getShape(string $shape)
    {
        // TODO: Implement getShape() method.
        return null;
    }

    public function getColor(string $color)
    {
        // TODO: Implement getColor() method.
        if ($color == null) {
            return null;
        }
        $type = strtoupper($color);
        if ($type == "RED") {
            return new Red();
        } else if ($type == "BLUE") {
            return new Blue();
        } else if ($type == "GREEN") {
            return new Green();
        }
        return null;
    }
}

class FactoryProducer
{
    public static function getFactory(string $factory)
    {
        $type = strtoupper($factory);
        if ($type == "SHAPE") {
            return new ShapeFactory();
        } else if ($type == "COLOR") {
            return new ColorFactory();
        }
        return null;
    }
}

$shapeFactory = FactoryProducer::getFactory('shape');
$shapeFactory->getShape("CIRCLE")->draw();
echo PHP_EOL;
$colorFactory = FactoryProducer::getFactory('color');
$colorFactory->getColor("blue")->fill();